package logic

import (
	"context"
	"fmt"
	"user_svc/models/mysql"

	"user_svc/internal/svc"
	"user_svc/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type LoginLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewLoginLogic(ctx context.Context, svcCtx *svc.ServiceContext) *LoginLogic {
	return &LoginLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 登录
func (l *LoginLogic) Login(in *user.LoginRequest) (*user.LoginResponse, error) {
	// todo: add your logic here and delete this line

	m := mysql.User{
		Account:  in.Account,
		Mobile:   in.Mobile,
		Password: in.Password,
	}
	mysql.DB.Debug().Where("mobile = ?", in.Mobile).Find(&m)
	if m.ID == 0 {
		mysql.DB.Debug().Create(&m)
		return nil, fmt.Errorf("账号未注册,自动生成账号")
	}
	if m.Password != in.Password {
		return nil, fmt.Errorf("密码错误")
	}

	return &user.LoginResponse{
		Id: int64(m.ID),
	}, nil
}
